package org.example.memora.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.responses.*;
import lombok.extern.slf4j.Slf4j;
import org.example.memora.DTO.UserRedisDTO;
import org.example.memora.common.result.Result;
import org.example.memora.common.utils.UserHolder;
import org.example.memora.entity.PostLike;
import org.example.memora.service.PostLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 点赞控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/likes")
@Tag(name = "帖子点赞管理", description = "提供对帖子点赞与取消点赞的操作接口")
public class PostLikeController {

    @Autowired
    private PostLikeService postLikeService;

    /**
     * 点赞帖子
     *
     *
     * @param postId 帖子 ID
     * @return 通用的 Result 对象，data 一般为 1 表示成功
     */
    @Operation(
            summary = "点赞帖子",
            description = "对指定帖子进行点赞操作，如已点赞则可能会返回错误信息。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "点赞成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping
    public Result<?> likePost(
            @Parameter(name = "postId", description = "帖子 ID（MongoDB 的 _id 或数据库自增ID）",
                    in = ParameterIn.QUERY, required = true)
            @RequestParam String postId
    ) {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 对帖子 [{}] 进行点赞", userId, postId);

        return postLikeService.likePost(userId, postId);
    }

    /**
     * 取消点赞
     *
     *
     * @param postId 帖子 ID
     * @return 通用的 Result 对象，data 一般为 1 表示成功
     */
    @Operation(
            summary = "取消点赞",
            description = "取消对指定帖子的点赞。如果尚未点赞则可能不会执行任何操作。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "取消点赞成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping
    public Result<?> unlikePost(
            @Parameter(name = "postId", description = "被取消点赞的帖子 ID",
                    in = ParameterIn.QUERY, required = true)
            @RequestParam String postId
    ) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 对帖子 [{}] 取消点赞", userId, postId);

        return postLikeService.unlikePost(userId, postId);
    }

    /**
     * 查询是否已点赞
     *
     *
     * @param postId 帖子 ID
     * @return 通用的 Result，对应 data 带“已点赞”或“未点赞”信息
     */
    @Operation(
            summary = "查询是否已点赞",
            description = "查询指定用户是否对某帖子已点赞。"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功（已点赞或未点赞）",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录或权限不足"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping
    public Result<?> hasLiked(
            @Parameter(name = "postId", description = "帖子 ID",
                    in = ParameterIn.QUERY, required = true)
            @RequestParam String postId
    ) {
        UserRedisDTO userRedisDTO =UserHolder.getUser();

        if(userRedisDTO==null){
            return  Result.error("获取不到UserID,请检查controller");
        }
        Long userId= userRedisDTO.getUserId();
        log.info("用户 [{}] 查询帖子 [{}] 是否已点赞", userId, postId);
        boolean liked = postLikeService.hasLiked(userId, postId);
        if (liked) {
            return Result.success(1, "已点赞");
        }
        return Result.success(1, "未点赞");
    }
    @Operation(
            summary = "获取帖子的点赞记录",
            description = "根据帖子 ID 查询所有点赞该帖子的用户记录"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "400", description = "参数错误"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/records")
    public Result<?> getLikesByPostId(
            @Parameter(name = "postId", description = "帖子 ID", required = true, in = ParameterIn.QUERY)
            @RequestParam String postId
    ) {
        return postLikeService.getLikeRecord(postId);
    }

    @Operation(
            summary = "我赞过的帖子",
            description = "查询当前登录用户点赞过的所有帖子"
    )
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "查询成功",
                    content = @Content(mediaType = "application/json")),
            @ApiResponse(responseCode = "401", description = "用户未登录"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/my-liked-posts")
    public Result<?> getMyLikedPosts() {
        UserRedisDTO userRedisDTO = UserHolder.getUser();

        if (userRedisDTO == null) {
            return Result.error("获取不到UserID,请检查controller");
        }

        Long userId = userRedisDTO.getUserId();
        log.info("用户 [{}] 查询自己点赞过的帖子", userId);

        return postLikeService.getLikedPostsByUser(userId);
    }


}
